package function;

import exception.ageException;
import exception.numberException;
import studentInformation.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author LinLe
 * @create 2020-12-17 17:22
 */
public class InformationOperateImpl implements InformationOperate {
    private List<Student> stu = new ArrayList<>();
    @Override
    public void inputInfo(){
        File file = new File("E:\\workspace_idea1\\module4-1\\src\\resources\\studentInformation");
        if(file.length()>0) {
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(file));
                stu.addAll((ArrayList<Student>) ois.readObject());
            } catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    @Override
    public void outputInfo(){
        ObjectOutputStream fos = null;
        try {
            fos =  new ObjectOutputStream(new FileOutputStream("E:\\workspace_idea1\\\\module4-1\\src\\resources\\studentInformation"));
            /*for (Student s: operate.getStu()
            ) {
                fos.writeObject(s);
            }*/
            fos.writeObject(stu);
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public List<Student> getStu() {
        return stu;
    }
    
    private Scanner sn = new Scanner(System.in);
    public void add() throws numberException, ageException {
        System.out.println("请输入学生名字：");
        String name = sn.next();
        System.out.println("请输入学生年龄：");
        int age = sn.nextInt();
        System.out.println("请输入学生学号：");
        String studentNumber = sn.next();
        addStudent(new Student(name,age, studentNumber));
    }

    @Override
    public void delete() {
        System.out.println("请输入要删除的学生的名字：");
        String name = sn.next();
        deleteStudent(name);
    }

    @Override
    public void find() {
        System.out.println("请输入要找的学生的名字：");
        String name = sn.next();
        Student s = findStudent(name);
        if(  s == null) {
            System.out.println("没有此人");
        }else{
            System.out.println("学生的信息如下："+ s);
        }
    }

    @Override
    public void change() throws numberException, ageException {
        System.out.println("请输入要修改的学生的名字：");
        String name = sn.next();
        int i =0;
        for (Student s: stu
        ) {
            if( s.getName().equals(name)){
                System.out.println("学生的信息如下："+ s + "\n" + "进入修改内容：");

                System.out.println("请输入学生名字：");
                String na = sn.next();
                System.out.println("请输入学生年龄：");
                int age = sn.nextInt();
                System.out.println("请输入学生学号：");
                String studentNumber = sn.next();
                changeStudent(s,na,age,studentNumber);
                System.out.println("修改成功");
                return;
            }
            i++;
        }
        if(i > stu.size()){
            System.out.println("没有此人");
        }
    }

    @Override
    public void show() {
        if(stu.size() == 0){
            System.out.println("还没有人哦！！！");
            return;
        }
        for (Student s: stu
             ) {
            System.out.println(s);
        }
    }

    private void addStudent(Student student) {
        stu.add(student);
    }
    private void deleteStudent(String name) {
        int i = 0;
        for (Student s: stu
             ) {
            if( s.getName().equals(name)){
                stu.remove(s);
                System.out.println("删除成功");
                return;
            }
            i++;
        }
        if(i > stu.size()){
            System.out.println("没有此人");
        }
    }

    private Student findStudent(String name) {
        for (Student s: stu
        ) {
            if( s.getName().equals(name)){

                return s;
            }
        }
        return null;
    }


    private void changeStudent(Student s, String name, int age, String studentNumber) throws ageException, numberException {
        s.setName(name);
        s.setAge(age);
        s.setStudentNumber(studentNumber);
    }

}
